<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Kr_akun_level2 extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('m_kr_akun_level1');
        $this->load->model('m_kr_akun_level2');
        $this->load->library('form_validation');
        $this->form_validation->set_message('required', '%s is required.');
        $this->form_validation->set_message('min_length', '%s minimal %s karakter.');
        $this->form_validation->set_message('max_length', '%s maksimal %s karakter.');
        $this->form_validation->set_message('is_unique', '%s telah terdaftar');
        $this->form_validation->set_message('is_numeric', '%s harus numeric');
        $this->form_validation->set_message('matches', '%s tidak cocok dengan %s.');
        $this->form_validation->set_message('numeric', '%s harus diisi angka.');
        $this->form_validation->set_message('alpha', '%s harus diisi abjad');
    }

    public function index() {
        $data['data'] = $this->m_kr_akun_level2->view();
        $this->load->view('kr_akun_level2/view', $data);
    }

    public function add() {
        $this->form_validation->set_rules('kode', 'kode', 'trim|required|max_length[4]|is_unique[kr_akun_level2.kode]');
        $this->form_validation->set_rules('uraian', 'uraian', 'trim|required|is_unique[kr_akun_level2.uraian]');
        $this->form_validation->set_rules('kr_akun_level1_kode', 'uraian akun level1', 'required');
                
        if($this->form_validation->run()) {
            
            $data = array(
                    'kode'                  =>  $this->input->post('kode'),
                    'uraian'                =>  $this->input->post('uraian'),
                    'kr_akun_level1_kode'   =>  $this->input->post('kr_akun_level1_kode'),
                    );
            
            $is_success = !$this->m_kr_akun_level2->insert($data);		
                    if ($is_success) {
                            $this->session->set_flashdata("message", "Data berhasil disimpan !");				
                    } else {
                            $this->session->set_flashdata("message", "Maaf, data tidak dapat disimpan !");				
                    }
            redirect('kr_akun_level2');
        } else {	
            $param['selected_kr_akun_level1_kode']     = $this->input->post('kr_akun_level1_kode');
            $param['options_kr_akun_level1_kode']      = $this->m_kr_akun_level1->get_options_kr_akun_level1_kode( array("" => "Select Uraian Kr_Akun_Level1") );
            
            $this->load->view('kr_akun_level2/add',$param);
        }
    }
    
    public function edit($kd) {
        $this->form_validation->set_rules('kode', 'kode', 'max_length[4]');
        
        if ($this->form_validation->run()) {
            $is_success = $this->m_kr_akun_level2->update($kd);		
            if ($is_success) {
                    $this->session->set_flashdata("message", "Data berhasil diupdate !");				
            } else {
                    $this->session->set_flashdata("message", "Maaf, data tidak dapat diupdate !");				
            }
            redirect('kr_akun_level2');
        }
        else {	
            $param['selected_kr_akun_level1_kode']    = $this->input->post('kr_akun_level1');
            $param['options_kr_akun_level1_kode']     = $this->m_kr_akun_level1->get_options_kr_akun_level1_kode();
           
            $param['data'] = $this->m_kr_akun_level2->get_by_kd($kd);
            $this->load->view('kr_akun_level2/edit',$param);
        }
    }
    
    public function delete($kd){
        $this->m_kr_akun_level2->delete($kd);
        redirect('kr_akun_level2');
    }    
    
   
}
?>
